home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / perl5 / HTTP / Status.pm < prev   
Text File  |  2009-06-13  |  9KB  |  255 lines

  1. package HTTP::Status;
  2.  
  3. use strict;
  4. require 5.002;   # because we use prototypes
  5.  
  6. use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION);
  7.  
  8. require Exporter;
  9. @ISA = qw(Exporter);
  10. @EXPORT = qw(is_info is_success is_redirect is_error status_message);
  11. @EXPORT_OK = qw(is_client_error is_server_error);
  12. $VERSION = "5.817";
  13.  
  14. # Note also addition of mnemonics to @EXPORT below
  15.  
  16. # Unmarked codes are from RFC 2616
  17. # See also: http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
  18.  
  19. my %StatusCode = (
  20.     100 => 'Continue',
  21.     101 => 'Switching Protocols',
  22.     102 => 'Processing',                      # RFC 2518 (WebDAV)
  23.     200 => 'OK',
  24.     201 => 'Created',
  25.     202 => 'Accepted',
  26.     203 => 'Non-Authoritative Information',
  27.     204 => 'No Content',
  28.     205 => 'Reset Content',
  29.     206 => 'Partial Content',
  30.     207 => 'Multi-Status',                    # RFC 2518 (WebDAV)
  31.     300 => 'Multiple Choices',
  32.     301 => 'Moved Permanently',
  33.     302 => 'Found',
  34.     303 => 'See Other',
  35.     304 => 'Not Modified',
  36.     305 => 'Use Proxy',
  37.     307 => 'Temporary Redirect',
  38.     400 => 'Bad Request',
  39.     401 => 'Unauthorized',
  40.     402 => 'Payment Required',
  41.     403 => 'Forbidden',
  42.     404 => 'Not Found',
  43.     405 => 'Method Not Allowed',
  44.     406 => 'Not Acceptable',
  45.     407 => 'Proxy Authentication Required',
  46.     408 => 'Request Timeout',
  47.     409 => 'Conflict',
  48.     410 => 'Gone',
  49.     411 => 'Length Required',
  50.     412 => 'Precondition Failed',
  51.     413 => 'Request Entity Too Large',
  52.     414 => 'Request-URI Too Large',
  53.     415 => 'Unsupported Media Type',
  54.     416 => 'Request Range Not Satisfiable',
  55.     417 => 'Expectation Failed',
  56.     422 => 'Unprocessable Entity',            # RFC 2518 (WebDAV)
  57.     423 => 'Locked',                          # RFC 2518 (WebDAV)
  58.     424 => 'Failed Dependency',               # RFC 2518 (WebDAV)
  59.     425 => 'No code',                         # WebDAV Advanced Collections
  60.     426 => 'Upgrade Required',                # RFC 2817
  61.     449 => 'Retry with',                      # unofficial Microsoft
  62.     500 => 'Internal Server Error',
  63.     501 => 'Not Implemented',
  64.     502 => 'Bad Gateway',
  65.     503 => 'Service Unavailable',
  66.     504 => 'Gateway Timeout',
  67.     505 => 'HTTP Version Not Supported',
  68.     506 => 'Variant Also Negotiates',         # RFC 2295
  69.     507 => 'Insufficient Storage',            # RFC 2518 (WebDAV)
  70.     509 => 'Bandwidth Limit Exceeded',        # unofficial
  71.     510 => 'Not Extended',                    # RFC 2774
  72. );
  73.  
  74. my $mnemonicCode = '';
  75. my ($code, $message);
  76. while (($code, $message) = each %StatusCode) {
  77.     # create mnemonic subroutines
  78.     $message =~ tr/a-z \-/A-Z__/;
  79.     $mnemonicCode .= "sub HTTP_$message () { $code }\n";
  80.     $mnemonicCode .= "*RC_$message = \\&HTTP_$message;\n";  # legacy
  81.     $mnemonicCode .= "push(\@EXPORT_OK, 'HTTP_$message');\n";
  82.     $mnemonicCode .= "push(\@EXPORT, 'RC_$message');\n";
  83. }
  84. eval $mnemonicCode; # only one eval for speed
  85. die if $@;
  86.  
  87. # backwards compatibility
  88. *RC_MOVED_TEMPORARILY = \&RC_FOUND;  # 302 was renamed in the standard
  89. push(@EXPORT, "RC_MOVED_TEMPORARILY");
  90.  
  91. %EXPORT_TAGS = (
  92.    constants => [grep /^HTTP_/, @EXPORT_OK],
  93.    is => [grep /^is_/, @EXPORT, @EXPORT_OK],
  94. );
  95.  
  96.  
  97. sub status_message  ($) { $StatusCode{$_[0]}; }
  98.  
  99. sub is_info         ($) { $_[0] >= 100 && $_[0] < 200; }
  100. sub is_success      ($) { $_[0] >= 200 && $_[0] < 300; }
  101. sub is_redirect     ($) { $_[0] >= 300 && $_[0] < 400; }
  102. sub is_error        ($) { $_[0] >= 400 && $_[0] < 600; }
  103. sub is_client_error ($) { $_[0] >= 400 && $_[0] < 500; }
  104. sub is_server_error ($) { $_[0] >= 500 && $_[0] < 600; }
  105.  
  106. 1;
  107.  
  108.  
  109. __END__
  110.  
  111. =head1 NAME
  112.  
  113. HTTP::Status - HTTP Status code processing
  114.  
  115. =head1 SYNOPSIS
  116.  
  117.  use HTTP::Status qw(:constants :is status_message);
  118.  
  119.  if ($rc != HTTP_OK) {
  120.      print status_message($rc), "\n";
  121.  }
  122.  
  123.  if (is_success($rc)) { ... }
  124.  if (is_error($rc)) { ... }
  125.  if (is_redirect($rc)) { ... }
  126.  
  127. =head1 DESCRIPTION
  128.  
  129. I<HTTP::Status> is a library of routines for defining and
  130. classifying HTTP status codes for libwww-perl.  Status codes are
  131. used to encode the overall outcome of a HTTP response message.  Codes
  132. correspond to those defined in RFC 2616 and RFC 2518.
  133.  
  134. =head1 CONSTANTS
  135.  
  136. The following constant functions can be used as mnemonic status code
  137. names.  None of these are exported by default.  Use the C<:constants>
  138. tag to import them all.
  139.  
  140.    HTTP_CONTINUE                        (100)
  141.    HTTP_SWITCHING_PROTOCOLS             (101)
  142.    HTTP_PROCESSING                      (102)
  143.  
  144.    HTTP_OK                              (200)
  145.    HTTP_CREATED                         (201)
  146.    HTTP_ACCEPTED                        (202)
  147.    HTTP_NON_AUTHORITATIVE_INFORMATION   (203)
  148.    HTTP_NO_CONTENT                      (204)
  149.    HTTP_RESET_CONTENT                   (205)
  150.    HTTP_PARTIAL_CONTENT                 (206)
  151.    HTTP_MULTI_STATUS                    (207)
  152.  
  153.    HTTP_MULTIPLE_CHOICES                (300)
  154.    HTTP_MOVED_PERMANENTLY               (301)
  155.    HTTP_FOUND                           (302)
  156.    HTTP_SEE_OTHER                       (303)
  157.    HTTP_NOT_MODIFIED                    (304)
  158.    HTTP_USE_PROXY                       (305)
  159.    HTTP_TEMPORARY_REDIRECT              (307)
  160.  
  161.    HTTP_BAD_REQUEST                     (400)
  162.    HTTP_UNAUTHORIZED                    (401)
  163.    HTTP_PAYMENT_REQUIRED                (402)
  164.    HTTP_FORBIDDEN                       (403)
  165.    HTTP_NOT_FOUND                       (404)
  166.    HTTP_METHOD_NOT_ALLOWED              (405)
  167.    HTTP_NOT_ACCEPTABLE                  (406)
  168.    HTTP_PROXY_AUTHENTICATION_REQUIRED   (407)
  169.    HTTP_REQUEST_TIMEOUT                 (408)
  170.    HTTP_CONFLICT                        (409)
  171.    HTTP_GONE                            (410)
  172.    HTTP_LENGTH_REQUIRED                 (411)
  173.    HTTP_PRECONDITION_FAILED             (412)
  174.    HTTP_REQUEST_ENTITY_TOO_LARGE        (413)
  175.    HTTP_REQUEST_URI_TOO_LARGE           (414)
  176.    HTTP_UNSUPPORTED_MEDIA_TYPE          (415)
  177.    HTTP_REQUEST_RANGE_NOT_SATISFIABLE   (416)
  178.    HTTP_EXPECTATION_FAILED              (417)
  179.    HTTP_UNPROCESSABLE_ENTITY            (422)
  180.    HTTP_LOCKED                          (423)
  181.    HTTP_FAILED_DEPENDENCY               (424)
  182.    HTTP_NO_CODE                         (425)
  183.    HTTP_UPGRADE_REQUIRED                (426)
  184.    HTTP_RETRY_WITH                      (449)
  185.  
  186.    HTTP_INTERNAL_SERVER_ERROR           (500)
  187.    HTTP_NOT_IMPLEMENTED                 (501)
  188.    HTTP_BAD_GATEWAY                     (502)
  189.    HTTP_SERVICE_UNAVAILABLE             (503)
  190.    HTTP_GATEWAY_TIMEOUT                 (504)
  191.    HTTP_HTTP_VERSION_NOT_SUPPORTED      (505)
  192.    HTTP_VARIANT_ALSO_NEGOTIATES         (506)
  193.    HTTP_INSUFFICIENT_STORAGE            (507)
  194.    HTTP_BANDWIDTH_LIMIT_EXCEEDED        (509)
  195.    HTTP_NOT_EXTENDED                    (510)
  196.  
  197. =head1 FUNCTIONS
  198.  
  199. The following additional functions are provided.  Most of them are
  200. exported by default.  The C<:is> import tag can be used to import all
  201. the classification functions.
  202.  
  203. =over 4
  204.  
  205. =item status_message( $code )
  206.  
  207. The status_message() function will translate status codes to human
  208. readable strings. The string is the same as found in the constant
  209. names above.  If the $code is unknown, then C<undef> is returned.
  210.  
  211. =item is_info( $code )
  212.  
  213. Return TRUE if C<$code> is an I<Informational> status code (1xx).  This
  214. class of status code indicates a provisional response which can't have
  215. any content.
  216.  
  217. =item is_success( $code )
  218.  
  219. Return TRUE if C<$code> is a I<Successful> status code (2xx).
  220.  
  221. =item is_redirect( $code )
  222.  
  223. Return TRUE if C<$code> is a I<Redirection> status code (3xx). This class of
  224. status code indicates that further action needs to be taken by the
  225. user agent in order to fulfill the request.
  226.  
  227. =item is_error( $code )
  228.  
  229. Return TRUE if C<$code> is an I<Error> status code (4xx or 5xx).  The function
  230. return TRUE for both client error or a server error status codes.
  231.  
  232. =item is_client_error( $code )
  233.  
  234. Return TRUE if C<$code> is an I<Client Error> status code (4xx). This class
  235. of status code is intended for cases in which the client seems to have
  236. erred.
  237.  
  238. This function is B<not> exported by default.
  239.  
  240. =item is_server_error( $code )
  241.  
  242. Return TRUE if C<$code> is an I<Server Error> status code (5xx). This class
  243. of status codes is intended for cases in which the server is aware
  244. that it has erred or is incapable of performing the request.
  245.  
  246. This function is B<not> exported by default.
  247.  
  248. =back
  249.  
  250. =head1 BUGS
  251.  
  252. For legacy reasons all the C<HTTP_> constants are exported by default
  253. with the prefix C<RC_>.  It's recommended to use explict imports and
  254. the C<:constants> tag instead of relying on this.
  255.